﻿(function ($) {
    $.extend({
        bb_toggle_navbar_link_tooltip: function (id, visible, placement) {
            var el = document.getElementById(id);
            if (el !== null) {
                var instance = bootstrap.Tooltip.getInstance(el);
                if (instance) {
                    instance.dispose();
                }
                if (visible && el.dataset.title) {
                    instance = new bootstrap.Tooltip(el, {
                        title: el.dataset.title,
                        placement: placement
                    });
                }
            }
        },
        bb_leave_navbar_dropdown_toggle: function (el, obj) {
            //此处阻塞mouseleave事件，在mouseenter之后执行
            el.addEventListener("mouseleave", event => {
                window.setTimeout(function () {
                    obj.invokeMethodAsync('MouseLeaveHandler');
                }, 0);
            });
        },
        bb_leave_navbar_dropdown_menu: function (el, obj) {
            el.addEventListener("mouseleave", event => {
                //此处阻塞mouseleave事件，在mouseenter之后执行
                window.setTimeout(function () {
                    obj.invokeMethodAsync('FloatingLeaveHandler');
                }, 0);
            });

            //绑定滚动方法
            var setttings = {}, DOM = {
                scope: el,
                track: el.querySelector('.navbar-track'),
                thumb: el.querySelector('.navbar-thumb'),
            };

            // 绑定滚动事件
            DOM.scope.addEventListener('scroll', () => {
                if (setttings.scroll) {
                    DOM.thumb.style.top = DOM.scope.scrollTop / setttings.maxScrollTop * setttings.thumbMaxTop + 'px';
                }
            });

            // 响应容器和内容大小改变
            let update = () => {
                //设置滑块容器和内容大小
                Object.assign(setttings, {
                    scroll: DOM.scope.scrollHeight > DOM.scope.clientHeight,
                    maxScrollTop: DOM.scope.scrollHeight - DOM.scope.clientHeight,
                    clientWidth: DOM.scope.clientWidth,
                    clientHeight: DOM.scope.clientHeight,
                    scrollWidth: DOM.scope.scrollWidth,
                    scrollHeight: DOM.scope.scrollHeight,
                });
                //滑块大小
                setttings.thumbHeight = Math.max((setttings.clientHeight / setttings.scrollHeight) * setttings.clientHeight, 30);
                //最大滑动距离
                setttings.thumbMaxTop = setttings.clientHeight - setttings.thumbHeight;
                //最大滚动距离

                DOM.track.style.display = setttings.scroll ? 'block' : 'none';
                DOM.thumb.style.height = setttings.thumbHeight + 'px';

                if (setttings.scroll) {
                    DOM.thumb.style.top = DOM.scope.scrollTop / setttings.maxScrollTop * setttings.thumbMaxTop + 'px';
                }
            };

            // 首次执行
            update();

            // chrome 64+ 和最新的 Edge Insider版支持
            // 通过Mutation​Observer API可以支持到 FF44 +，IE9 +，Edge 10 + ，Safari 11 +
            new ResizeObserver((entries) => {
                let contentRect = entries[0].contentRect;
                if ((contentRect.width || contentRect.height)) {
                    update();
                }
            }).observe(DOM.scope);

        },
        bb_update_navbar_menu_indicator: function (id, el) {
            if (!el) el = document.getElementById(id);
            console.info(id + '|' + el.scrollHeight + '|' + el.clientHeight);
            //更新滚动指示器状态
            if (el.scrollHeight > el.clientHeight) {
                let height = Math.max((el.clientHeight / el.scrollHeight) * el.clientHeight, 42);
                let ratio = el.scrollTop / (el.scrollHeight - el.clientHeight);
                var top = ratio * (el.clientHeight - height) + el.offsetTop;
                el.style.setProperty('--navbar-menu-scroll-top', top + 'px');
                el.style.setProperty('--navbar-menu-scroll-height', height + 'px');
                el.style.setProperty('--navbar-menu-scroll-display', 'block');
            } else {
                el.style.setProperty('--navbar-menu-scroll-display', 'none');
            }
        },
        bb_init_navbar_menu_indicator: function (id) {
            var el = document.getElementById(id);

            // 绑定滚动事件
            el.addEventListener('scroll', e => $.bb_update_navbar_menu_indicator(id, el));

            // 首次执行
            $.bb_update_navbar_menu_indicator(id, el);

            // chrome 64+ 和最新的 Edge Insider版支持
            // 通过Mutation​Observer API可以支持到 FF44 +，IE9 +，Edge 10 + ，Safari 11 +
            new ResizeObserver((entries) => {
                let contentRect = entries[0].contentRect;
                if ((contentRect.width || contentRect.height)) {
                    $.bb_update_navbar_menu_indicator(id, el);
                }
            }).observe(el);
        },
    });
})(jQuery);
